<?php
Class AuthManageAction extends Action {

	public function _initialize() {
		$this->MenuModel     = D('Menu');
		$this->AuthRuleModel = D('AuthRule');
	}

	/**
	 * 权限列表
	 */
	public function index() {
		$data = D('AuthManage')->getGroupList();
		$this->assign('data' , $data);
		$this->display();
	}

	/**
	 * 更新权限规则 
	 * to do 
	 */
	public function updateRules() {
		//1.获取菜单节点
		$data          = $this->menuNodes();
		$insertNodos   = array();
		$AuthRuleModel = M('AuthRule');
		//2.循环菜单节点
		foreach ($data as $value) {
			//处理顶级菜单和节点菜单
			if($value['pid'] == 0) {
				$value['type'] = AuthRuleModel::RULE_MAIN;
			}else{
				$value['type'] = AuthRuleModel::RULE_URL;
			}
			//验证key
			$key = strtolower($value['name'] . $value['module'] .$value['type']);
			$insertNodos[$key]           = $value;
			$insertNodos[$key]['status'] = 1;
		}

		//3.获取权限规则
		$rules = $AuthRuleModel->where()->select();

		foreach ($rules as $index => $value) {
			$key = strtolower($value['name'] . $value['module'] .$value['type']);
			//判断菜单节点是否同时存在key
			if(isset($insertNodos[strtolower($key)])) {
				$insertNodos[$key]['id'] = $value['id']; 
				$update[]                = $insertNodos[$key];
				unset($insertNodos[$key]);
				unset($rules[$index]);
				unset($value['condition']);
				$diff[$value['id']] = $value;		
			}elseif($value['status'] == 1){
				$ids[] = $value['id'];
			}
		}
		//4.对已经存在权限规则判断是否需要更新
        if (count($update)) {
            foreach ($update as $k => $row) {
                if ($row != $diff[$row['id']]) {
                    $AuthRuleModel->where(array('id' => $row['id']))->save($row);
                }
            }
        }
        //5.对权限规则进行逻辑删除
		if(count($ids)) {
			$AuthRuleModel->where(array('id'=> array('IN',implode(',' , $ids))))->save(array('status' => -1));
		}
		//6.批量添加权限规则
		if(count($insertNodos)) {
			$AuthRuleModel->addAll(array_values($insertNodos));
		}
	}

	/**
	 * 获取菜单节点
	 * @return [type] [description]
	 */
	public function menuNodes() {
		$menuNodos = $this->MenuModel->getList();
		foreach ($menuNodos as $key => $value) {
			$module                = explode('/', $value['url']);
			$nodes[$key]['title']  = $value['name'];
			$nodes[$key]['name']   = $value['url'];
			$nodes[$key]['module'] = strtolower($module[0]);
			$nodes[$key]['pid']    = $value['parent'];
		}
		return $nodes;
	}

	/**
	 * 访问授权
	 */
	public function access() {
		$this->updateRules();
		//1.获取用户组的权限规则
		$gid           = I('get.gid', 0 , 'intval');
		if(!$gid) {
			$this->error('参数错误,请检查！');
		}
		$group         = D('AuthManage')->getGroupRow(array('id'=>$gid ,'status'=>1));
		if(!$group) {
			$this->error('该用户组可能已经被删除或者禁止！');
		}
		$rules         = explode(',', $group['rules']);
		$rules         = array_map('intval', $rules);
		//2.获取树形菜单节点
		$menuNodes     = $this->MenuModel->getList(array('status'=>1));
		$menuNodes     = listToTree($menuNodes , 'id' ,'parent');
		
		//3.获取顶级规则
		$mainMap       = array('status'=>1 , 'type'=>1);
		$mainRule      = $this->AuthRuleModel->getListByField($mainMap, 'name,id');

		//4.获取子级规则
		$childMap      = array('status'=>1 , 'type'=>2);
		$childRule     = $this->AuthRuleModel->getListByField($childMap ,'name,id');

	
		$this->assign('group_rules'  , $rules);
		$this->assign('menu_nodes'   , $menuNodes);
		$this->assign('main_rules'   , $mainRule);
		$this->assign('child_rules'  , $childRule);
		//p($childRule);
		$this->display();
		
	}

	public function advanced() {
		$this->display();
	}
	
}